package com.example.wwk.graduationpractice.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.wwk.graduationpractice.Activity.LoginActivity;
import com.example.wwk.graduationpractice.Activity.QuizActivity;
import com.example.wwk.graduationpractice.Adapter.QuestionAdapter;
import com.example.wwk.graduationpractice.Bean.QuestionBean;
import com.example.wwk.graduationpractice.Globalutils.CustomDialogUtils;
import com.example.wwk.graduationpractice.Globalutils.PromptShowUtils;
import com.example.wwk.graduationpractice.Globalutils.CustomLinearLayoutManager;
import com.example.wwk.graduationpractice.NetDataUtils.DynamicDataUtils;
import com.example.wwk.graduationpractice.R;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

/**
 * Created by wwk on 2017/4/24.
 */

public class tab_dynamic extends Fragment {
    private Context context;
    //recyclerView
    private List<QuestionBean> questionBeanList = new ArrayList<>();
    private List<QuestionBean> questionBeen = new ArrayList<>();
    private QuestionAdapter questionAdapter;
    private RecyclerView recyclerView;
    //加载框
    private CustomDialogUtils progressDialog;
    //刷新
    private SwipeRefreshLayout swipeRefresh;
    private boolean refreshing = false;
    //intent
    private static String thisuserName;
    private static int identity;
    //handler
    private static final int REFRESH = 888;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DynamicDataUtils.GET_QUESTION_SUCCESS:
                    questionBeen = (List<QuestionBean>) msg.obj; //转换从网络获得的数据的类型
                    //初始化问题列表的数据
                    initDatas();
                    if (!refreshing) {
                        questionAdapter.notifyDataSetChanged();
                        progressDialog.dismiss();
                    }
                    break;
                case REFRESH:
                    new Handler().postDelayed(new Runnable() {     //Handler实现延迟刷新UI
                        @Override
                        public void run() {
                            questionAdapter.notifyDataSetChanged();
                            swipeRefresh.setRefreshing(false);
                            PromptShowUtils.showSnackbar(swipeRefresh, "刷新成功！");
                        }
                    }, 1000);        //秒
            }
        }

    };

    /*
    * 初始化实例
    */
    public static tab_dynamic newInstance(String thisuserName2, int identity2) {
        tab_dynamic fragment = new tab_dynamic();
        thisuserName = thisuserName2;
        identity = identity2;
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab_dynamic, container, false);
        //加载网络获得数据的进度条
        progressDialog = new CustomDialogUtils(context, R.style.CustomDialog);
        progressDialog.setCancelable(false);        //无法手动取消
        progressDialog.show();
        //fab
        FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
        if (identity == LoginActivity.DOCTOR)
            fab.setVisibility(View.GONE);
        else
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, QuizActivity.class);
                    intent.putExtra("thisuserName", thisuserName);
                    startActivityForResult(intent, 1);//1为请求码
                }
            });
        //swipe刷新
        swipeRefresh = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh);
        swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
        swipeRefresh.setOnRefreshListener(new MyRefreshListener());
        //RecyclerView
        DynamicDataUtils.getQuestions(handler);       //初始化网络数据
        questionAdapter = new QuestionAdapter(questionBeanList, thisuserName, identity);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        final CustomLinearLayoutManager layoutManager = new CustomLinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(questionAdapter);
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    /*
    初始化显示的数据
    */
    private void initDatas() {
        questionBeanList.clear();
        for (QuestionBean questionBean : questionBeen) {
            questionBeanList.add(questionBean);
        }
    }

    /*
    * 获得返回问题，用于显示
    */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {   //请求码
            case 1:
                if (resultCode == RESULT_OK) {// 接收数据
                    QuestionBean questionBean = data.getParcelableExtra("questionBean");
                    updateList(questionBean);
                }
                break;
        }
    }

    private void updateList(QuestionBean questionBean) {
        questionBeanList.add(0, questionBean);       //添加置顶
        questionAdapter.notifyDataSetChanged();
        recyclerView.scrollToPosition(0);

    }

    class MyRefreshListener implements SwipeRefreshLayout.OnRefreshListener {        //swipeRefresh监听事件
        @Override
        public void onRefresh() {
            refreshList();
        }
    }

    private void refreshList() {
        DynamicDataUtils.getQuestions(handler);
        refreshing = true;
        Message message = new Message();
        message.what = REFRESH;
        handler.sendMessage(message);

    }
}